package org.middleware.controller.system;


import lombok.extern.slf4j.Slf4j;
import org.middleware.common.AuthenticationToken.RequireLogin;
import org.middleware.common.BusinessException;
import org.middleware.controller.common.CommonHttpResponse;
import org.middleware.entity.system.SysMenus;
import org.middleware.service.system.SysMenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequireLogin
@RestController
@Slf4j
public class SysMenusController {

    @Autowired
    private SysMenusService menusService;

    /**
     * 根据系统id和父菜单id查询菜单
     * @param system_id  系统id
     * @param parent_id 父菜单id
     * @return 菜单列表
     */
    @GetMapping("/menus/system/{system_id}/parent/{parent_id}/")
    public CommonHttpResponse getMenus(@PathVariable int system_id, @PathVariable int parent_id) {
        try {
            List<SysMenus> menus = menusService.getMenusBySysAndParent(system_id, parent_id, "");
            return CommonHttpResponse.success(menus);
        } catch (BusinessException e) {
            log.error("菜单查询失败：", e);
            return CommonHttpResponse.fail(400, "请求参数错误");
        } catch (Exception e) {
            log.error("菜单查询失败：", e);
            return CommonHttpResponse.fail(500, e.getMessage());
        }
    }

    @GetMapping("/menus/systems/{system_id}/parents/{parent_id}/name/{name:.*}?")
    public CommonHttpResponse getMenus(@PathVariable int system_id, @PathVariable int parent_id,
                                       @PathVariable(value = "name", required = false) String name) {
        try {
            List<SysMenus> menus = menusService.getMenusBySysAndParent(system_id, parent_id, name);
            return CommonHttpResponse.success(menus);
        } catch (BusinessException e) {
            log.error("菜单查询失败：", e);
            return CommonHttpResponse.fail(400, "请求参数错误");
        } catch (Exception e) {
            log.error("菜单查询失败：", e);
            return CommonHttpResponse.fail(500, e.getMessage());
        }
    }

    /**
     * 根据系统id查询菜单
     * @param system 系统id
     * @return 菜单树
     */
    @GetMapping("/menus/system/{system}/tree")
    public CommonHttpResponse getMenuTree(@PathVariable int system) {
        try {
            List<SysMenus> result =menusService.getMenuTreeBySystem(system);
            return CommonHttpResponse.success(result);
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        } catch (Exception e) {
            log.error("菜单查询异常:", e);
            return CommonHttpResponse.fail(500, "菜单查询异常，请联系管理员");
        }
    }

    /**
     * 新增菜单
     * @param menu 菜单数据
     * @return 操作的记录数
     */
    @PostMapping("/menus")
    public CommonHttpResponse createMenu(@RequestBody SysMenus menu) {
        try {
            menusService.create(menu);
            return CommonHttpResponse.success("菜单新增成功");
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        } catch (Exception e) {
            log.error("菜单新增异常:", e);
            return CommonHttpResponse.fail(500, "菜单新增异常，请联系管理员");
        }
    }

    /**
     * 修改菜单信息
     * @param menu 菜单信息
     * @return
     */
    @PutMapping("/menus")
    public CommonHttpResponse update(@RequestBody SysMenus menu) {
        try {
            menusService.update(menu);
            return CommonHttpResponse.success("菜单修改成功");
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        } catch (Exception e) {
            log.error("菜单修改异常:", e);
            return CommonHttpResponse.fail(500, "菜单修改异常，请联系管理员");
        }
    }

    /**
     * 删除菜单
     * @param id 菜单id
     * @return
     */
    @DeleteMapping("/menus/{id}")
    public CommonHttpResponse update(@PathVariable int id) {
        try {
            menusService.deleteAllSubMenus(id);
            return CommonHttpResponse.success("菜单删除成功");
        } catch (BusinessException e) {
            return CommonHttpResponse.fail(400, e.getMessage());
        } catch (Exception e) {
            log.error("菜单删除异常:", e);
            return CommonHttpResponse.fail(500, "菜单删除异常，请联系管理员");
        }
    }


}
